<?php
/**
 * [WeEngine System] Copyright (c) 2014 WE7.CC
 * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
 */
defined('IN_IA') or exit('Access Denied');
load()->model('setting');

$dos = array('display', 'buy', 'update_time');
$do = in_array($do, $dos) ? $do : 'display';

$copyright = $_W['setting']['copyright'];
$settings = $_W['setting']['register'];

if ('display' == $do) {
	if ($_W['ispost']) {
		$is_copyright = false;
		switch ($_GPC['key']) {
			case 'open':
				$settings['open'] = intval($_GPC['value']);
				break;
			case 'verify':
				$settings['verify'] = intval($_GPC['value']);
				break;
			case 'groupid':
				$settings['groupid'] = intval($_GPC['value']);
				break;
			case 'safe':
				$settings['safe'] = intval($_GPC['value']);
				break;
			case 'service_expire_status':
				$settings['service_expire_status'] = intval($_GPC['value']) ? STATUS_ON : STATUS_OFF;
				if ($settings['service_expire_status']) {
					if (!isset($settings['service_expire_time'])) {
						$settings['service_expire_time'] = 1;
					}
					if (!isset($settings['service_expire_price'])) {
						$settings['service_expire_price'] = 1;
					}
					cloud_set_user_service($settings['service_expire_time'], $settings['service_expire_price']);
				}
				break;
			case 'service_expire_time':
				$settings['service_expire_time'] = intval($_GPC['value']);
				if ($settings['service_expire_time'] < 1) {
					iajax(-1, '有效天数最小值须为1！');
				}
				if (!empty($settings['service_expire_status'])) {
					if (!isset($settings['service_expire_price'])) {
						$settings['service_expire_price'] = 1;
					}
					cloud_set_user_service($settings['service_expire_time'], $settings['service_expire_price']);
				}
				break;
			case 'service_expire_price':
				$settings['service_expire_price'] = intval($_GPC['value']);
				if ($settings['service_expire_price'] < 1) {
					iajax(-1, '价格最小值须为1！');
				}
				if (!empty($settings['service_expire_status'])) {
					if (!isset($settings['service_expire_time'])) {
						$settings['service_expire_time'] = 1;
					}
					cloud_set_user_service($settings['service_expire_time'], $settings['service_expire_price']);
				}
				break;
			case 'verifycode':
				$copyright['verifycode'] = intval($_GPC['value']);
				$is_copyright = true;
				break;
			case 'refused_login_limit':
				$copyright['refused_login_limit'] = intval($_GPC['value']);
				$is_copyright = true;
				break;
		}
		if ($is_copyright) {
			setting_save($copyright, 'copyright');
		} else {
			setting_save($settings, 'register');
			cache_delete(cache_system_key('defaultgroupid', array('uniacid' => $_W['uniacid'])));
		}
		iajax(0, '更新设置成功！');
	}

	$groups = user_group();
	if (empty($groups)) {
		$groups = array(array('id' => "0", 'name' => '请选择所属用户组'));
	} else {
		array_unshift($groups, array('id' => "0", 'name' => '请选择所属用户组'));
	}

	$group = array();
	foreach ($groups as $item) {
		if ($item['id'] == $settings['groupid']) {
			$group = $item;
			break;
		}
	}
	if (empty($group)) {
		$group = array('id' => "0", 'name' => '请选择所属用户组');
		$settings['groupid'] = 0;
		setting_save($settings, 'register');
	}
	if ($_W['isajax']) {
		$message = array(
			'settings' => $settings,
			'default_group' => $group,
			'groups' => $groups,
			'verifycode' => empty($copyright['verifycode']) ? '' : $copyright['verifycode'],
			'refused_login_limit' => $copyright['refused_login_limit'],
			'copyright' => $copyright
		);
		iajax(0, $message);
	}
}

if ('buy' == $do) {
	$ticket = cloud_get_user_service_transactions_ticket();
	if (is_error($ticket)) {
		iajax(-1, $ticket['message']);
	}
	iajax(0, $ticket);
}

if ('update_time' == $do) {
	$user = user_single($_W['uid']);
	if (empty($user)) {
		iajax(-1, '访问错误, 未找到该用户.');
	}
	if (!empty($_W['setting']['register']['service_expire_time'])) {
		$end_time = strtotime($_W['setting']['register']['service_expire_time'] . ' days');
		pdo_update('users', array('endtime' => $end_time), array('uid' => $user['uid']));
		user_related_update($user['uid'], array('endtime' => $end_time));
	}
	cache_delete(cache_system_key('cloud_get_user_service_if_expired:' . $_W['user']['console_uid']));
	iajax(0, '更新成功！');
}
